﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient; //for SqlConnection
using System.Data;
using ModelLayer;

namespace DBLayer
{
    public class DBSeat
    {
        private SqlConnection con;

        public DBSeat()
        {
            con = DBConnection.getInstance().getDBCon();
        }

        public List<Seat> GetAllSeatsOnShow(int show_id)
        {
            List<Seat> seats = new List<Seat>();

            SqlDataReader reader = null;

            SqlParameter idParam = new SqlParameter("@show_id", SqlDbType.Int);
            idParam.Value = show_id;

            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandText = "SELECT id, status, show_id FROM Seat WHERE show_id = @show_id";
            com.Parameters.Add(idParam);


            try
            {
                reader = com.ExecuteReader();

                while (reader.Read())
                {
                    seats.Add(new Seat(reader.GetInt32(0), reader.GetString(1), reader.GetInt32(2)));
                }

                reader.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR: " + e);
            }

            return seats;
        }

        public Seat GetSeat(int seatId)
        {
            Seat seat = null;
            SqlDataReader r = null;
            SqlParameter seatPram = new SqlParameter("@Id", SqlDbType.Int);
            seatPram.Value = seatId;

            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandText = "SELECT Id, Status, show_id FROM Seat WHERE Id = @Id";
            com.Parameters.Add(seatPram);

            try
            {
                r = com.ExecuteReader();

                while (r.Read())
                {
                    seat = new Seat(r.GetInt32(0), r.GetString(1), r.GetInt32(2));
                }
                r.Close();
            }
            catch (Exception e)
            {

                Console.WriteLine("ERROR: " + e);
            }
            return seat;
        }
        //public List<Seat> GetSeatByShowroomId(int showroom)
        //{
        //    List<Seat> seat = new List<Seat>();
        //    SqlDataReader = null;

        //}

        //En metode til at låse et sæde
        public int LockSeat(int seatId, int status)
        {

            int result = -1; //int to store affected rows in, -1 as default when no rows have been changed
            SqlParameter seatParam = new SqlParameter("@Id", SqlDbType.Int);
            seatParam.Value = seatId;

            SqlParameter statusParam = new SqlParameter("@status", SqlDbType.Int, 1);
            statusParam.Value = status;

            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandText = "UPDATE Seat SET status = @status WHERE id = @Id AND status = 0";
            com.Parameters.Add(seatParam);
            com.Parameters.Add(statusParam);

            try
            {
                result = com.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                Console.WriteLine("Error: " + e);
            }
            return result;
         }

        public bool UnLockSeat(int seatId)
        {
            bool updated = false;
            SqlParameter seatParam = new SqlParameter("@Id", SqlDbType.Int);
            seatParam.Value = seatId;

            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandText = "UPDATE Seat SET status = 0 WHERE id = @Id";
            com.Parameters.Add(seatParam);

            try
            {
                if (com.ExecuteNonQuery() != 0)
                {
                    updated = true;
                }
            }
            catch (SqlException e)
            {
                Console.WriteLine("Error: " + e);
            }

            return updated;

        }

    }
}
